package com.newa.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.newa.beans.Work;
import com.newa.dao.DAOSection;
import com.newa.utils.HibUtil;
import com.opensymphony.xwork2.ActionSupport;

public class deleteSection extends ActionSupport {
	private Logger LOG = Logger.getLogger(deleteSection.class);
	private int section_id;

	@Override
	public String execute() throws Exception {
		Session session = HibUtil.getSessionFactory().openSession();
		DAOSection daos = new DAOSection(session);
		try {
			Set<Work> list = daos.getSectionById(section_id).getWORKS();

			for (Work work : list) {

				String lLink = work.getLOGO_LINK();
				String phLinks = work.getPHOTO_LINKS();
				String lphLinks = work.getLPHOTO_LINKS();
				if (lLink != null && lLink != "") {
					File d = new File(lLink);
					d.delete();
				}
				if (phLinks != null && phLinks != "") {
					StringTokenizer tok = new StringTokenizer(phLinks, ";");
					StringTokenizer ltok = new StringTokenizer(lphLinks, ";");
					while (tok.hasMoreElements()) {
						String pa = (String) tok.nextElement();
						String lpa = ltok.nextToken();
						File ld = new File(lpa);
						File d = new File(pa);
						ld.delete();
						d.delete();
					}
				}
			}
			daos.deleteSectionById(section_id);
		} catch (Exception e) {
			LOG.warn(e);
			throw new RuntimeException(e);
		} finally {
			daos.closeSession().close();
		}
		return "success";
	}

	public int getSection_id() {
		return section_id;
	}

	public void setSection_id(int section_id) {
		this.section_id = section_id;
	}

}
